// MeasureLength

import { DT } from "dt-sdk/library/DT"

/*
绘制线
 */
class MeasureLength {
  constructor(arg) {
    this.viewer = arg.viewer
    this.Cesium = arg.Cesium
    this.callback = arg.callback
    this._polyline = null //活动线
    this._polylineLast = null //最后一条线
    this._positions = []  //活动点
    this._entities_point = []  //脏数据
    this._entities_line = []  //脏数据
    this._polylineData = null //用于构造线数据
  }

  //返回最后活动线
  get line() {
    return this._polylineLast
  }

  //返回线数据用于加载线
  getData() {
    return this._polylineData
  }

  //加载线
  loadPolyline(data, option) {
    var $this = this
    var polyline = this.viewer.dataSourceDisplay._defaultDataSource.entities.add({
      polyline: {
        positions: data,
        show: true,
        material: $this.Cesium.Color.fromCssColorString(option.color),
        width: 3,
        clampToGround: true
      }
    })
    return polyline
  }

  //开始创建
  startCreate(option) {
    
    // console.log(this.viewer.cesiumViewer)
    // debugger
    var $this = this
    // this.handler = new this.Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas)
    this.handler = new this.Cesium.ScreenSpaceEventHandler(this.viewer.cesiumViewer.canvas)
    this.handler.setInputAction(function(evt) { //单机开始绘制
     
      //屏幕坐标转地形上坐标
      var cartesian = $this.getCatesian3FromPX(evt.position)
      if (!cartesian) return

      if ($this._positions.length == 0) {
        $this._positions.push(cartesian.clone())
      }
      $this._positions.push(cartesian)
      $this.createPoint(cartesian)// 绘制点
    }, $this.Cesium.ScreenSpaceEventType.LEFT_CLICK)
    this.handler.setInputAction(function(evt) { //移动时绘制线
      if ($this._positions.length < 1) return
      var cartesian = $this.getCatesian3FromPX(evt.endPosition)
      if (!cartesian) return

      if (!$this.Cesium.defined($this._polyline)) {
        $this._polyline = $this.createPolyline(option)
      }
      if ($this._polyline) {
        $this._positions.pop()
        $this._positions.push(cartesian)
      }
    }, $this.Cesium.ScreenSpaceEventType.MOUSE_MOVE)
    this.handler.setInputAction(function(evt) {
      if (!$this._polyline) return
      var cartesian = $this.getCatesian3FromPX(evt.position)
      if (!cartesian) return

      $this._positions.pop()
      $this._positions.push(cartesian)
      $this.createPoint(cartesian)// 绘制点
      $this._polylineData = $this._positions.concat()
      $this.viewer.dataSourceDisplay._defaultDataSource.entities.remove($this._polyline) //移除
      $this._polyline = null
      $this._positions = []
      var line = $this.loadPolyline($this._polylineData, option) //加载线

      var length = 0;
      var measurePositions = line._polyline.positions._value;
      for (let i = 0; i < measurePositions.length - 1; i++) {
        
        var point1 = $this.Cesium.Cartographic.fromCartesian( measurePositions[i])
        point1.height = 0.0;
        var point2 = $this.Cesium.Cartographic.fromCartesian(measurePositions[i+1])
        point2.height = 0.0;
        var geoD = new $this.Cesium.EllipsoidGeodesic();
        geoD.setEndPoints(point1, point2)
        var innerS = geoD.surfaceDistance;
        length+=innerS;

      }
      // console.log(`${length}米`)

      // var labelPt = measurePositions[measurePositions.length -1 ]

      $this._entities_line.push(line)


      $this._polylineLast = line
      console.log($this.callback)
      if (typeof $this.callback == 'function') {
        // $this.callback(line)
        $this.callback(`${length.toFixed(2)}米`)
      }
      $this.clearPoint()
      //取消绘制
      $this.destroy()
    }, $this.Cesium.ScreenSpaceEventType.RIGHT_CLICK)
  }

  //创建点
  createPoint(cartesian) {
    var $this = this
    var point = this.viewer.dataSourceDisplay._defaultDataSource.entities.add({
      position: cartesian,
      point: {
        pixelSize: 10,
        color: $this.Cesium.Color.YELLOW
      }
    })
    $this._entities_point.push(point)
    return point
  }

  //创建线
  createPolyline(option) {
    var $this = this
    var polyline = this.viewer.dataSourceDisplay._defaultDataSource.entities.add({
      polyline: {
        //使用cesium的peoperty
        positions: new $this.Cesium.CallbackProperty(function() {
          return $this._positions
        }, false),
        show: true,
        material: $this.Cesium.Color.fromCssColorString(option.color),
        width: 3,
        clampToGround: true
      }
    })
    $this._entities_line.push(polyline)
    
    return polyline
  }

  clearPoint() {
    for (var i = 0; i < this._entities_point.length; i++) {
      this.viewer.dataSourceDisplay._defaultDataSource.entities.remove(this._entities_point[i])
    }
    this._entities_point = []  //脏数据
  }

  //销毁
  destroy() {
    if (this.handler) {
      this.handler.destroy()
      this.handler = null
    }
  }

  //清空实体对象
  clear() {
    for (var i = 0; i < this._entities_point.length; i++) {
      this.viewer.dataSourceDisplay._defaultDataSource.entities.remove(this._entities_point[i])
    }
    for (var i = 0; i < this._entities_line.length; i++) {
      this.viewer.dataSourceDisplay._defaultDataSource.entities.remove(this._entities_line[i])
    }
    this._polyline = null
    this._positions = []
    this._entities_point = []  //脏数据
    this._entities_line = []  //脏数据
    this._polylineData = null //用于构造线数据
    this._polylineLast = null
  }

  getCatesian3FromPX(px) {
    var cartesian
    var ray = this.viewer.cesiumViewer.camera.getPickRay(px)
    if (!ray) return null
    cartesian = this.viewer.cesiumViewer.scene.globe.pick(ray, this.viewer.cesiumViewer.scene)
    if (!cartesian) {
      cartesian = this.viewer.cesiumViewer.camera.pickEllipsoid(px, this.viewer.cesiumViewer.scene.globe.ellipsoid)
    }
    return cartesian
  }
}

export default MeasureLength
